using FSM.IDal; 
using FSM.IBll; 
using FSM.Model; 
using SqlSugar; 
using System; 
using System.Collections.Generic; 
using System.Text;
using System.IO;
using Util.Excel;

namespace FSM.Bll 
{ 
    public class LogEventBll: ILogEventBll 
    { 
        #region 模块 
        private readonly ILogEventDal dal;
        private readonly IExcelAssistant _excel;
        public LogEventBll(ILogEventDal dal, IExcelAssistant excel) 
        { 
            this.dal = dal;
            this._excel = excel;
        } 
        #endregion 
 
        #region 查询 
 
        /// <summary> 
        /// 根据键值查询单个信息 
        /// </summary> 
        /// <param name="pkid">键值</param> 
        /// <returns></returns> 
        public log_event Get(int? pkid) 
        { 
            return dal.GetById(pkid); 
        } 
 
        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="eventId">事件id</param> 
        /// <param name="message">消息</param> 
        /// <param name="page">页码</param> 
        /// <param name="page_size">每页显示的条数</param> 
        /// <returns></returns> 
        public Tuple<List<log_event>, int> ListPagin(int? eventId, string message, int page, int page_size) 
        { 
            ISugarQueryable<log_event> queryable = dal.CreateQueryable();
            queryable = queryable.WhereIF(eventId != null, p => p.eventId == eventId);
            queryable = queryable.WhereIF(!string.IsNullOrEmpty(message),p => p.message == message);
            queryable = queryable.OrderBy("operatingTime desc");
            PageModel pm = new PageModel 
            { 
                PageIndex = page, 
                PageSize = page_size 
            }; 
            Tuple<List<log_event>, int> result = dal.GetPageListAndTotal(queryable, pm); 
            return result; 
        }

        /// <summary> 
        /// 分页查询批量信息 
        /// </summary> 
        /// <param name="eventId">事件id</param> 
        /// <param name="message">消息</param> 
        /// <returns></returns> 
        public List<log_event> List(int? eventId, string message)
        {
            Expressionable<log_event> queryable = Expressionable.Create<log_event>();
            queryable = queryable.AndIF(eventId != null, p => p.eventId == eventId);
            queryable = queryable.AndIF(!string.IsNullOrEmpty(message), p => p.message == message);

            List<log_event> result = dal.GetList(queryable.ToExpression());
            return result;
        }

        /// <summary>
        /// 获得要导出的数据的Excel流
        /// </summary>
        /// <param name="eventId">事件ID</param>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public byte[] Export(int? eventId, string message)
        {
            List<log_event> result = List(eventId, message);
            return _excel.Collection2ExcelBytes(result, "日志");
        }
        #endregion 
    } 
} 
